package com.ws.webpacket.view;

import android.content.Context;
import android.content.res.Resources;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;


import androidx.annotation.Nullable;


import com.ws.webpacket.util.HLog;

import static java.lang.Math.abs;

public class FloatButton extends View {

    private float touchStartLeft =0;
    private float touchStartTop = 0;
    private int startLeft = 0;
    private int startTop = 0;

    private int windowWidth = 0;
    private int windowHeight = 0;

    private boolean isMoving = false;

    //当移动范围超过当前值时才算移动
    private static final float OVER_CLICK_X = 10;
    private static final float OVER_CLICK_Y = 10;

    private  int newX ;
    private  int newY;


    public FloatButton(Context context) {
        super(context);
        init();
    }

    public FloatButton(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public FloatButton(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        Resources resources = this.getResources();
        DisplayMetrics dm = resources.getDisplayMetrics();
        windowWidth = dm.widthPixels ;
        windowHeight = dm.heightPixels ;
    }

    @Override
    protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout (boolean changed, int left, int top, int right, int bottom) {
            super.onLayout(changed,  left,  top,  right,  bottom);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();
        float x =  event.getX();
        float y  = event.getY();
        switch (action){
            case MotionEvent.ACTION_DOWN:
                touchStartLeft = x+getLeft();
                touchStartTop = y+getTop();
                startLeft = getLeft();
                startTop = getTop();
                isMoving = false;
                break;
            case MotionEvent.ACTION_UP:
                float movedX = abs(x+getLeft() - touchStartLeft);
                float movedY = abs(y+getTop() - touchStartTop);
                if(isMoving && (OVER_CLICK_X < movedX || OVER_CLICK_Y < movedY)){

                    isMoving = false;

                    ViewGroup.MarginLayoutParams params  = (ViewGroup.MarginLayoutParams) getLayoutParams();
                    params.topMargin = newY;
                    params.leftMargin = newX;
                    setLayoutParams(params);

                    return  true;
                }
                break;
            case MotionEvent.ACTION_MOVE:
                isMoving = true;
                newX = (int)(getLeft()+x - touchStartLeft + startLeft);
                newY = (int)(getTop()+y-touchStartTop+startTop);
                if(newX<0)newX = 0;
                if(newY<0) newY = 0;
                if((newX+getWidth()) > windowWidth){
                    newX = windowWidth - getWidth();
                }
                if((newY+getHeight()) > windowHeight){
                    newY  = windowHeight -getHeight();
                }
                layout(newX,newY ,newX+getWidth(),newY+getHeight());
                break;
        }
        return super.onTouchEvent(event);
    }



}
